Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./year2.RDS")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2021-06-30"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2021-06-30"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.25, n = 393)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 11.44645 11.49492 11.54261 11.58955 11.63575 11.68125 11.72607 11.77022
##   [9] 11.81373 11.85662 11.89893 11.94066 11.98184 12.02250 12.06265 12.10229
##  [17] 12.14137 12.17986 12.21772 12.25493 12.29145 12.32725 12.36229 12.39655
##  [25] 12.42999 12.46259 12.49430 12.52550 12.55654 12.58730 12.61768 12.64758
##  [33] 12.67688 12.70550 12.73332 12.76024 12.78616 12.81097 12.83457 12.85686
##  [41] 12.87772 12.89873 12.92105 12.94404 12.96704 12.98938 13.01040 13.02945
##  [49] 13.04586 13.05897 13.06999 13.08044 13.09021 13.09917 13.10720 13.11416
##  [57] 13.11993 13.12439 13.12740 13.12885 13.12859 13.12652 13.12249 13.11639
##  [65] 13.10652 13.09177 13.07294 13.05079 13.02611 12.99967 12.97226 12.94465
##  [73] 12.91763 12.89196 12.86843 12.84782 12.82509 12.79586 12.76194 12.72510
##  [81] 12.68714 12.64983 12.61498 12.58437 12.55978 12.53770 12.51373 12.48835
##  [89] 12.46202 12.43520 12.40836 12.38196 12.35647 12.33236 12.31008 12.29011
##  [97] 12.27292 12.25768 12.24327 12.22967 12.21690 12.20494 12.19378 12.18342
## [105] 12.17385 12.16507 12.15707 12.14985 12.14340 12.13771 12.13279 12.13006
## [113] 12.13047 12.13331 12.13785 12.14335 12.14911 12.15438 12.15844 12.16058
## [121] 12.16249 12.16617 12.17135 12.17775 12.18509 12.19309 12.20148 12.20998
## [129] 12.21831 12.22619 12.23334 12.23949 12.24436 12.24767 12.24913 12.24891
## [137] 12.24739 12.24497 12.24204 12.23898 12.23620 12.23408 12.23301 12.23339
## [145] 12.23561 12.24006 12.24562 12.25102 12.25635 12.26174 12.26729 12.27312
## [153] 12.27935 12.28608 12.29342 12.30151 12.31043 12.32031 12.33127 12.34340
## [161] 12.35684 12.37168 12.38989 12.41273 12.43927 12.46860 12.49979 12.53192
## [169] 12.56406 12.59529 12.62468 12.65133 12.67429 12.69264 12.71120 12.73479
## [177] 12.76258 12.79371 12.82731 12.86256 12.89858 12.93453 12.96956 13.00281
## [185] 13.03343 13.06057 13.08337 13.10099 13.11257 13.11726 13.11704 13.11456
## [193] 13.10993 13.10325 13.09465 13.08423 13.07212 13.05841 13.04324 13.02670
## [201] 13.00892 12.99001 12.96653 12.93567 12.89846 12.85592 12.80907 12.75896
## [209] 12.70660 12.65302 12.59925 12.54632 12.49525 12.44708 12.40283 12.36353
## [217] 12.32419 12.28009 12.23270 12.18344 12.13376 12.08511 12.03893 11.99666
## [225] 11.95974 11.92518 11.88925 11.85236 11.81488 11.77723 11.73978 11.70293
## [233] 11.66708 11.63261 11.59992 11.56940 11.54145 11.51645 11.49479 11.47482
## [241] 11.45481 11.43507 11.41589 11.39758 11.38046 11.36482 11.35096 11.33920
## [249] 11.32984 11.32317 11.31952 11.32003 11.32526 11.33459 11.34744 11.36320
## [257] 11.38126 11.40103 11.42191 11.44329 11.46457 11.48515 11.50443 11.52181
## [265] 11.53668 11.55383 11.57731 11.60538 11.63633 11.66845 11.70001 11.72929
## [273] 11.75458 11.77415 11.79106 11.80933 11.82867 11.84881 11.86947 11.89038
## [281] 11.91125 11.93182 11.95181 11.97094 11.98893 12.00550 12.02020 12.03308
## [289] 12.04463 12.05531 12.06564 12.07608 12.08712 12.09926 12.11297 12.12719
## [297] 12.14060 12.15327 12.16530 12.17678 12.18777 12.19839 12.20871 12.21881
## [305] 12.22879 12.23873 12.24872 12.25884 12.26918 12.27983 12.29088 12.30241
## [313] 12.31450 12.32725 12.34203 12.35962 12.37924 12.40008 12.42137 12.44230
## [321] 12.46208 12.47992 12.49502 12.50916 12.52447 12.54071 12.55764 12.57501
## [329] 12.59259 12.61013 12.62739 12.64413 12.66010 12.67507 12.68879 12.70102
## [337] 12.71152 12.71979 12.72577 12.72983 12.73234 12.73369 12.73425 12.73439
## [345] 12.73450 12.73495 12.73612 12.73838 12.74212 12.74654 12.75063 12.75443
## [353] 12.75799 12.76135 12.76456 12.76765 12.77066 12.77365 12.77666 12.77972
## [361] 12.78287 12.78617 12.78966 12.79316 12.79650 12.79971 12.80282 12.80587
## [369] 12.80888 12.81189 12.81493 12.81803 12.82114 12.82418 12.82717 12.83011
## [377] 12.83300 12.83585 12.83866 12.84143 12.84418 12.84689 12.84959 12.85227
## [385] 12.85495 12.85764 12.86033 12.86301 12.86568 12.86832 12.87092 12.87348
## [393] 12.87599
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./site_objects/wrf_a_year2.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.25, n = 393)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 10.78438 10.86881 10.95171 11.03306 11.11284 11.19102 11.26760 11.34254
##   [9] 11.41583 11.48745 11.55738 11.62559 11.69208 11.75681 11.81977 11.88096
##  [17] 11.94042 11.99819 12.05429 12.10876 12.16161 12.21289 12.26262 12.31083
##  [25] 12.35756 12.40282 12.44666 12.48876 12.52885 12.56702 12.60335 12.63791
##  [33] 12.67080 12.70209 12.73187 12.76022 12.78721 12.81294 12.83748 12.86092
##  [41] 12.88334 12.90320 12.91935 12.93248 12.94324 12.95232 12.96038 12.96809
##  [49] 12.97614 12.98518 12.99350 12.99915 13.00241 13.00361 13.00304 13.00101
##  [57] 12.99782 12.99378 12.98920 12.98437 12.97961 12.97522 12.97150 12.96877
##  [65] 12.96653 12.96405 12.96126 12.95807 12.95440 12.95017 12.94530 12.93972
##  [73] 12.93334 12.92608 12.91786 12.90860 12.89786 12.88545 12.87172 12.85697
##  [81] 12.84153 12.82573 12.80989 12.79432 12.77937 12.76496 12.75075 12.73657
##  [89] 12.72227 12.70768 12.69265 12.67703 12.66065 12.64336 12.62500 12.60541
##  [97] 12.58444 12.55981 12.52994 12.49575 12.45813 12.41801 12.37628 12.33386
## [105] 12.29165 12.25056 12.21150 12.17538 12.14309 12.11556 12.09369 12.07207
## [113] 12.04581 12.01657 11.98602 11.95582 11.92765 11.90317 11.88406 11.87197
## [121] 11.86527 11.86107 11.85914 11.85926 11.86121 11.86475 11.86968 11.87576
## [129] 11.88276 11.89048 11.89867 11.90712 11.91561 11.92391 11.93384 11.94709
## [137] 11.96317 11.98163 12.00200 12.02381 12.04660 12.06991 12.09327 12.11621
## [145] 12.13826 12.15897 12.18102 12.20707 12.23657 12.26898 12.30379 12.34045
## [153] 12.37843 12.41718 12.45619 12.49491 12.53281 12.56936 12.60401 12.63624
## [161] 12.66551 12.69129 12.71502 12.73848 12.76158 12.78428 12.80650 12.82818
## [169] 12.84926 12.86967 12.88935 12.90823 12.92625 12.94334 12.96146 12.98225
## [177] 13.00524 13.02991 13.05576 13.08229 13.10901 13.13542 13.16101 13.18528
## [185] 13.20774 13.22788 13.24521 13.25923 13.26943 13.27531 13.27885 13.28216
## [193] 13.28499 13.28705 13.28809 13.28784 13.28604 13.28241 13.27669 13.26862
## [201] 13.25793 13.24435 13.22646 13.20344 13.17592 13.14455 13.10996 13.07279
## [209] 13.03368 12.99326 12.95218 12.91108 12.87058 12.83134 12.79398 12.75914
## [217] 12.72270 12.68104 12.63557 12.58771 12.53887 12.49047 12.44392 12.40063
## [225] 12.36203 12.32431 12.28316 12.23921 12.19310 12.14547 12.09694 12.04815
## [233] 11.99973 11.95233 11.90658 11.86310 11.82253 11.78552 11.75269 11.72147
## [241] 11.68917 11.65629 11.62328 11.59064 11.55883 11.52833 11.49963 11.47319
## [249] 11.44949 11.42901 11.41223 11.39898 11.38863 11.38092 11.37564 11.37253
## [257] 11.37136 11.37190 11.37390 11.37713 11.38135 11.38632 11.39180 11.39757
## [265] 11.40337 11.41170 11.42453 11.44084 11.45959 11.47977 11.50034 11.52028
## [273] 11.53856 11.55417 11.56939 11.58699 11.60660 11.62785 11.65040 11.67387
## [281] 11.69790 11.72214 11.74623 11.76979 11.79248 11.81393 11.83713 11.86459
## [289] 11.89514 11.92766 11.96101 11.99403 12.02560 12.05458 12.07982 12.10286
## [297] 12.12598 12.14913 12.17226 12.19533 12.21830 12.24112 12.26375 12.28613
## [305] 12.30823 12.33000 12.35140 12.37237 12.39288 12.41288 12.43232 12.45116
## [313] 12.46936 12.48686 12.50382 12.52042 12.53667 12.55258 12.56817 12.58346
## [321] 12.59844 12.61315 12.62758 12.64186 12.65604 12.67011 12.68401 12.69772
## [329] 12.71121 12.72443 12.73735 12.74993 12.76214 12.77395 12.78532 12.79621
## [337] 12.80659 12.81559 12.82260 12.82798 12.83206 12.83520 12.83775 12.84005
## [345] 12.84246 12.84532 12.84897 12.85378 12.86008 12.86742 12.87508 12.88300
## [353] 12.89109 12.89930 12.90755 12.91579 12.92393 12.93192 12.93968 12.94715
## [361] 12.95426 12.96094 12.96713 12.97305 12.97896 12.98484 12.99064 12.99634
## [369] 13.00190 13.00729 13.01247 13.01742 13.02219 13.02684 13.03139 13.03581
## [377] 13.04011 13.04428 13.04832 13.05222 13.05598 13.05959 13.06305 13.06635
## [385] 13.06947 13.07238 13.07509 13.07762 13.07998 13.08218 13.08424 13.08616
## [393] 13.08797
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./site_objects/wrf_b_year2.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.25, n = 393)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 10.85938 10.91926 10.97814 11.03599 11.09282 11.14862 11.20339 11.25711
##   [9] 11.30978 11.36139 11.41193 11.46141 11.50980 11.55712 11.60334 11.64851
##  [17] 11.69268 11.73584 11.77799 11.81912 11.85922 11.89830 11.93633 11.97333
##  [25] 12.00927 12.04416 12.07799 12.11060 12.14187 12.17185 12.20060 12.22817
##  [33] 12.25463 12.28001 12.30439 12.32780 12.35031 12.37198 12.39285 12.41298
##  [41] 12.43243 12.45124 12.46938 12.48680 12.50344 12.51924 12.53414 12.54809
##  [49] 12.56102 12.57287 12.58385 12.59416 12.60377 12.61264 12.62072 12.62799
##  [57] 12.63440 12.63991 12.64449 12.64810 12.65070 12.65225 12.65272 12.65206
##  [65] 12.64970 12.64526 12.63897 12.63109 12.62186 12.61151 12.60031 12.58849
##  [73] 12.57629 12.56396 12.55174 12.53989 12.52555 12.50656 12.48413 12.45950
##  [81] 12.43390 12.40855 12.38470 12.36357 12.34638 12.33011 12.31129 12.29045
##  [89] 12.26815 12.24494 12.22136 12.19798 12.17533 12.15397 12.13444 12.11730
##  [97] 12.10310 12.09106 12.07997 12.06973 12.06024 12.05138 12.04305 12.03516
## [105] 12.02758 12.02021 12.01296 12.00571 11.99836 11.99080 11.98293 11.97523
## [113] 11.96819 11.96172 11.95574 11.95016 11.94489 11.93985 11.93495 11.93010
## [121] 11.92578 11.92246 11.91998 11.91820 11.91698 11.91618 11.91565 11.91525
## [129] 11.91483 11.91425 11.91337 11.91205 11.91014 11.90750 11.90271 11.89488
## [137] 11.88471 11.87290 11.86015 11.84714 11.83457 11.82314 11.81353 11.80645
## [145] 11.80259 11.80263 11.80488 11.80726 11.80987 11.81284 11.81628 11.82030
## [153] 11.82503 11.83058 11.83707 11.84461 11.85332 11.86332 11.87472 11.88765
## [161] 11.90220 11.91851 11.93839 11.96299 11.99148 12.02299 12.05667 12.09167
## [169] 12.12714 12.16223 12.19608 12.22785 12.25667 12.28171 12.30786 12.33997
## [177] 12.37709 12.41829 12.46261 12.50910 12.55684 12.60486 12.65222 12.69798
## [185] 12.74120 12.78092 12.81620 12.84610 12.86966 12.88596 12.89838 12.91073
## [193] 12.92267 12.93387 12.94397 12.95264 12.95955 12.96435 12.96670 12.96626
## [201] 12.96269 12.95565 12.94303 12.92355 12.89812 12.86765 12.83305 12.79523
## [209] 12.75512 12.71361 12.67163 12.63008 12.58988 12.55193 12.51716 12.48647
## [217] 12.45357 12.41302 12.36702 12.31775 12.26742 12.21821 12.17232 12.13193
## [225] 12.09925 12.06986 12.03821 12.00482 11.97022 11.93491 11.89942 11.86425
## [233] 11.82993 11.79696 11.76588 11.73718 11.71139 11.68903 11.67061 11.65668
## [241] 11.64703 11.64098 11.63786 11.63699 11.63771 11.63933 11.64120 11.64263
## [249] 11.64296 11.64150 11.63760 11.63286 11.62928 11.62677 11.62526 11.62466
## [257] 11.62488 11.62585 11.62748 11.62969 11.63238 11.63549 11.63892 11.64260
## [265] 11.64643 11.65054 11.65511 11.66016 11.66572 11.67180 11.67843 11.68563
## [273] 11.69341 11.70181 11.71086 11.72053 11.73073 11.74138 11.75239 11.76367
## [281] 11.77513 11.78669 11.79825 11.80974 11.82105 11.83211 11.84417 11.85823
## [289] 11.87377 11.89029 11.90726 11.92419 11.94055 11.95585 11.96956 11.98312
## [297] 11.99818 12.01455 12.03204 12.05049 12.06969 12.08947 12.10965 12.13003
## [305] 12.15044 12.17070 12.19061 12.21000 12.22867 12.24646 12.26317 12.27862
## [313] 12.29262 12.30500 12.31548 12.32415 12.33137 12.33753 12.34298 12.34810
## [321] 12.35325 12.35881 12.36513 12.37173 12.37788 12.38361 12.38896 12.39395
## [329] 12.39862 12.40299 12.40710 12.41097 12.41464 12.41814 12.42150 12.42474
## [337] 12.42790 12.43102 12.43407 12.43704 12.43989 12.44259 12.44511 12.44742
## [345] 12.44949 12.45128 12.45276 12.45391 12.45470 12.45473 12.45376 12.45194
## [353] 12.44944 12.44643 12.44307 12.43951 12.43593 12.43249 12.42934 12.42666
## [361] 12.42461 12.42334 12.42302 12.42305 12.42275 12.42224 12.42160 12.42093
## [369] 12.42033 12.41990 12.41973 12.41992 12.42028 12.42060 12.42090 12.42119
## [377] 12.42150 12.42185 12.42227 12.42278 12.42340 12.42417 12.42509 12.42619
## [385] 12.42756 12.42923 12.43115 12.43330 12.43562 12.43808 12.44064 12.44325
## [393] 12.44587
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./site_objects/wrf_c_year2.rda")

keeping in case

#save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
#save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
#save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
#save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
#save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
#save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
#save(both_ymina, file = "./plotly_objs/both_ymina.rda")
#save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

#save(both_yminb, file = "./plotly_objs/both_yminb.rda")
#save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

#save(both_yminc, file = "./plotly_objs/both_yminc.rda")
#save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")